package com.whosly.strategy.scan;

import com.whosly.strategy.IStrategy;
import com.whosly.strategy.container.IStrategyContainer;
import com.whosly.strategy.manager.ContainerProxyManager;
import lombok.Getter;
import org.springframework.context.support.ApplicationObjectSupport;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * 具体容器的代理
 *
 * @Author yueny09 <deep_blue_yang@126.com>
 * @Date 2019-09-05 15:27
 */
public class ContainerProxy<C, S extends IStrategy<C>, T extends IStrategyContainer<C, S>>
        extends ApplicationObjectSupport
        implements InvocationHandler {

    /**
     * 策略容器
     */
    @Getter
    private Class<IStrategyContainer<C, S>> strategyContainerInterface;


    // 被匿名内部类实现的 IStrategyContainer interface
    private IStrategyContainer<C, S> strategyContainerProxy;

    public ContainerProxy(Class<IStrategyContainer<C, S>> strategyContainerInterface) {
        this.strategyContainerInterface = strategyContainerInterface;

//    private Class<C> conditon;
//    private Class<S> strategy;
     //        this.conditon = (Class<C>) GenericExtractor.getClass(StrategyContainerExtractor.getInterfaceType(strategyContainerInterface), 0);
     //        this.strategy = (Class<S>) GenericExtractor.getClass(StrategyContainerExtractor.getInterfaceType(strategyContainerInterface), 1);

        ContainerProxyManager.get().registry(strategyContainerInterface.getName(), this);
     }

     /**
     *
     * @param proxy
     * @param method eg. getStrategy
     * @param args eg. getStrategy的入参
     * @return
     * @throws Throwable
     */
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        if(args == null || args.length == 0) {
            throw new IllegalArgumentException("无效条件:"+ args);
        }

        if(strategyContainerProxy == null){
//            strategyContainerProxy = ContainerFactoryHolder.get().getRows()
        }
        C condition = (C)args[0];
        return strategyContainerProxy.getStrategy(condition);
    }
}
